﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
        public Client client;
        public SerialPort serial;
        public SerialPort sp = null;
        public Form3()
        {
            InitializeComponent();
            client = new Client(this);
            ipBox.Text = "127.0.0.1";
            comBox.Text = "30";
            idBox.Text = "12345";
            nameBox.Text = "hoi";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            signIn();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                signIn();
            } 
            else if (!Char.IsNumber(e.KeyChar) && (int)e.KeyChar != 8 )
            {
                e.Handled = true;
            }
            
        }

        private void nameBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar==13)
            {
                signIn();
            }
        }

        private void signIn()
        {
            int i;
            if (!int.TryParse(idBox.Text, out i))
            {
                MessageBox.Show("het getal is te groot");
                idBox.Text = "";
            }
            else if (idBox.Text == "" && nameBox.Text == "")
            {
                MessageBox.Show("Enter name and ID");
            }
            else if (idBox.Text == "" && nameBox.Text != "")
            {
                MessageBox.Show("Enter  ID");
            }
            else if (idBox.Text != "" && nameBox.Text == "")
            {
                MessageBox.Show("Enter  name");
            }
            else
            {
                if(sp == null)
                sp = connectBike();
                Boolean connected = client.connect(ipBox.Text, 60530);
                System.Diagnostics.Debug.WriteLine(connected);
                System.Diagnostics.Debug.WriteLine(sp);
                if (!connected)
                {
                    MessageBox.Show("Failed to connect the server...");
                }
                else if(sp == null)
                {
                    MessageBox.Show("Failed to connect the bike...");
                }
                else if(connected && sp !=null) 
                {
                    System.Diagnostics.Debug.WriteLine(connected + "    " + sp);
                    client.routeToServer("Fiets:" + idBox.Text + ":" + nameBox.Text);
                    new Form1(client, sp ).Show();
                    this.Hide();
                }
            }
        }

        private void ipBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                signIn();
            }
        }

        private void comBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((int)e.KeyChar == 13)
            {
                signIn();
            }
        }

        public SerialPort connectBike()
        {
            try
            {
                serial = new SerialPort("COM"+comBox.Text);
                serial.Open();
                return serial;
            }
            catch
            {
                return null;
            }            
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

    }
}
